package com.sunspring.jbug.controller;

import com.sunspring.jbug.bean.UserInfo;
import com.sunspring.jbug.common.RoleEnum;
import com.sunspring.jbug.common.SimpleResponse;
import com.sunspring.jbug.dto.count.BugCount;
import com.sunspring.jbug.param.ReportParam;
import com.sunspring.jbug.service.ReportService;
import com.sunspring.jbug.support.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Context;

/**
 * @Author: wulei
 * @Date: 2019/3/10
 */
@RestController
@RequestMapping("/statistics")
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 项目bug统计
     */
    @GetMapping("/projectBug")
    public SimpleResponse projectBugCount(ReportParam reportParam) {
        return SimpleResponse.success(reportService.getBug(reportParam));
    }

    /**
     * 我的项目bug统计
     */
    @GetMapping("/myBugCount")
    public SimpleResponse myBugCount(ReportParam reportParam) {
        UserInfo userInfo = ContextHolder.get();
        if (userInfo.getRole() == RoleEnum.RESTORER.getCode()) {
            reportParam.setRestorer(userInfo.getId());
        }else {
            reportParam.setSubmitter(userInfo.getId());
        }
        BugCount bugCount = reportService.getBug(reportParam);
        if (userInfo.getRole() == RoleEnum.RESTORER.getCode()) {
            bugCount.setReceived(bugCount.getCount());
        }else {
            bugCount.setAssign(bugCount.getCount());
        }
        return SimpleResponse.success(bugCount);
    }

    /**
     * 项目模板/版本数量统计
     */
    @GetMapping("/versionAndModule")
    public SimpleResponse versionAndModule(ReportParam reportParam) {
        return SimpleResponse.success(reportService.getVersionAndModule(reportParam));
    }

}
